package org.newlifecac;

import java.net.HttpURLConnection;
import java.net.URL;
import java.io.*;
import android.os.Environment;

public class DownloadAndStore
{
	private URL url;
	private String sdcard=Environment.getExternalStorageDirectory()+"/";
	
	public void download(String folderName, String urlString)
	{
		try
		{
			url = new URL(urlString);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			InputStream istream = connection.getInputStream();
			String filename = urlString
					.substring(urlString.lastIndexOf("/") + 1);
			File dir = new File(sdcard+folderName+"/");
			if (!dir.exists())
			{
				dir.mkdir();
			}
			File file = new File(sdcard+folderName+"/" + filename);
			file.createNewFile();

			OutputStream output = new FileOutputStream(file);
			byte[] buffer = new byte[1024 * 4];
			while (istream.read(buffer) != -1)
			{
				output.write(buffer);
			}
			output.flush();
			output.close();
			istream.close();
		} catch (Exception e)
		{
			e.printStackTrace();
		}

	}
}
